package com.aftersale.demo.utils;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.thread.ThreadUtil;
import com.aftersale.demo.entity.*;
import com.aftersale.demo.service.LogService;
import com.aftersale.demo.service.UserService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * @author 空
 * @date 2023/11/23 10:30
 * @desc
 */

@Component
@Aspect
@Slf4j
public class LogAspect {

    @Autowired
    LogService logService;
    @Autowired
    UserService userService;

    @AfterReturning(pointcut = "@annotation(dbLog)",returning = "returnValue")
    public void recordLog(JoinPoint joinPoint,DbLog dbLog,Object returnValue) {
        // 获取当前登录的用户的信息
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        int userId=MyUtil.getUserId(request);
        String userName="";
        Result result=null;
        StringBuffer details=new StringBuffer();
        if(returnValue instanceof Result){
            result=((Result) returnValue);
        }
        if(userId==-1){
            return;
        }else if(userId==0){
            Object[] objects=joinPoint.getArgs();
            if(objects[0] instanceof User){
                User user=(User) objects[0];
                userName=user.getUserName();
            }
            if(returnValue instanceof Result){
                 details.append(result.getMsg());
            }
        }else{
            userName=userService.getById(userId).getUserName();
            if(result.getCode().equals("200")){
                Object[] args=joinPoint.getArgs();
                for (Object arg : args) {
                    if(arg instanceof Problem){
                        Problem problem=(Problem) arg;
                        problem.setFile(null);
                        details.append(problem);
                    }else if(arg instanceof Solve){
                        Solve solve=(Solve) arg;
                        solve.setFile(null);
                        details.append(solve);
                    }else {
                        details.append(arg.toString()+"\n");
                    }

                }
            }else{
                details.append(result.getMsg());
            }

        }

        Log log = Log.builder()
                .operation(dbLog.operation())
                .type(dbLog.type().getValue())
                .userName(userName)
                .time(DateUtil.now())
                .details(details.toString())
                .build();

        ThreadUtil.execAsync(() -> {
            // 异步记录日志信息
            logService.save(log);
        });
    }

}
